package com.dingding.special.order.bill.biz.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.order.code.order.OrderStatus;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.special.base.enums.OrderEnums;
import com.dingding.special.order.aboard.code.ConfirmAboardCodeMsg;
import com.dingding.special.order.bill.biz.WaterBillService;
import com.dingding.special.order.bill.dao.WaterBillDao;
import com.dingding.special.order.bill.dto.OrderFinishAmountDTO;
import com.dingding.special.order.bill.dto.OrderFinishRateDTO;
import com.dingding.special.order.bill.dto.OrderParticularsDTO;
import com.dingding.special.order.bill.dto.ReqWaterBillDTO;
import com.dingding.special.order.bill.dto.ResOrderFinishAmount;
import com.dingding.special.order.bill.dto.ResOrderFinishRate;
import com.dingding.special.order.bill.dto.ResOrderParticulars;
import com.dingding.special.order.bill.dto.ResWaterBillDTO;

/** 
 * @ClassName: WaterBillServiceImpl 
 * @Description: 账单流水Service实现类
 * @author SUNZHICHENG 
 * @date 2016年8月22日 下午12:01:04 
 * @version v2.4.0
*/
@Component("waterBillService")
public class WaterBillServiceImpl implements WaterBillService {

	private Logger logger = Logger.getLogger(WaterBillServiceImpl.class);

	@Resource(name = "waterBillDao")
	private WaterBillDao waterBillDao;

	/**
	 * 
	 * @Title: queryWaterBill 
	 * @Description: 查询账单流水
	 * @param ReqWaterBillDTO 请求Bean(封装请求参数)
	 * @return ResponseEntity<ResWaterBillDTO> 
	 * @author SUNZHICHENG  
	 * @date 下午2:21:28
	 * @since v2.4.0
	 * @see 修改hufan
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public ResponseEntity<ResWaterBillDTO> queryWaterBill(ReqWaterBillDTO reqWaterBillDTO) {

		ResponseEntity<ResWaterBillDTO> responseEntity = new ResponseEntity<ResWaterBillDTO>();
		List<ResWaterBillDTO> countWaterBill = null;

		// 查询时间段内所有的账单流水
		try {
			countWaterBill = waterBillDao.queryWaterBill(reqWaterBillDTO);
			// 计算账单信息并封装返回
			ResWaterBillDTO billDTO = CalculateBill(countWaterBill,PublishCarType.SPECIAL);

			// 封装要响应的数据
			responseEntity.setCode(HttpStatus.SC_OK);
			responseEntity.setPromptMessage("请求成功");
			responseEntity.setResponseBody(billDTO);
		} catch (Exception e) {
			responseEntity.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			responseEntity.setErrorMessage("查询账单流水异常");
			logger.error("查询账单流水出现异常:" + this.getClass() + ",queryWaterBill()," + reqWaterBillDTO.toString(), e);
		}

		return responseEntity;
	}

	/**
	 * 
	 * @Title: queryTaxiWaterBill 
	 * @Description: 查询出租车账单流水
	 * @param reqWaterBillDTO 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResponseEntity<ResWaterBillDTO>    返回类型
	 * @see 修改 hufan
	 */
	@Override
	@Transactional(readOnly = true)
	public ResponseEntity<ResWaterBillDTO> queryTaxiWaterBill(ReqWaterBillDTO reqWaterBillDTO){
		
		ResponseEntity<ResWaterBillDTO> responseEntity = new ResponseEntity<ResWaterBillDTO>();
		
		List<ResWaterBillDTO> countWaterBill = null;

		// 查询时间段内所有的账单流水
		try {
			countWaterBill = waterBillDao.queryTaxiWaterBill(reqWaterBillDTO);
			// 计算账单信息并封装返回
			ResWaterBillDTO billDTO = CalculateBill(countWaterBill,PublishCarType.TAXI);

			// 封装要响应的数据
			responseEntity.setCode(HttpStatus.SC_OK);
			responseEntity.setPromptMessage("请求成功");
			responseEntity.setResponseBody(billDTO);
		} catch (Exception e) {
			responseEntity.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			responseEntity.setErrorMessage("查询账单流水异常");
			logger.error("查询账单流水出现异常:" + this.getClass() + ",queryWaterBill()," + reqWaterBillDTO.toString(), e);
		}

		return responseEntity;
	}
	
	/**
	 * 
	 * @Title: CalculateBill 
	 * @Description:计算账单信息并封装成响应Bean 
	 * @param List<ResWaterBillDTO> 数据库查询时间段内所有的账单流水
	 * @return ResWaterBillDTO 响应Bean
	 * @author SUNZHICHENG  
	 * @date  2016年8月23日 下午4:13:00
	 * @since v2.4.0
	 * @see 修改 HuFan
	 */
	private ResWaterBillDTO CalculateBill(List<ResWaterBillDTO> countWaterBill,PublishCarType carType) {
		
		int totalOrds = 0; // 总订单数
		int completionNumber = 0; // 指派订单完成数
		int cancellationNumber = 0; // 指派订单取消数
		BigDecimal completionTurnover = new BigDecimal(0.0); // 指派订单完成流水
		BigDecimal cancellationTurnover = new BigDecimal(0.0); // 指派订单取消流水
		BigDecimal totalTurnover = new BigDecimal(0.0); // 总流水
		String completionRate = null; // 订单完成率
		BigDecimal rate = new BigDecimal(0.0); // 比率计算临时变量
		// 格式化
		DecimalFormat df = new DecimalFormat("#####.00");
		// 计算完成、取消、总的流水
		if (countWaterBill != null && countWaterBill.size() > 0) {
			for (ResWaterBillDTO bill : countWaterBill) {
				// 判断来源
				if (carType == PublishCarType.SPECIAL) {
					// 已完成的账单流水
					if (bill.getOrderStatus() == OrderEnums.ALREADY_COMPLETE.getIndex()) {
						completionNumber++;
						if (bill.getTotalTurnover() != null) {
							completionTurnover = completionTurnover.add(bill.getTotalTurnover());
						}

						// 已取消的账单流水
					} else if (bill.getOrderStatus() == OrderEnums.ALREADY_CANCEL.getIndex()) {
						cancellationNumber++;
						if (bill.getTotalTurnover() != null) {
							cancellationTurnover = cancellationTurnover.add(bill.getTotalTurnover());
						}

						// 总的流水
					}
				} else if (carType == PublishCarType.TAXI) {
					// 已完成的账单流水
					if (bill.getOrderStatus() == OrderStatus.ORDER_STATUS_COMPLETE) {
						completionNumber++;
						if (bill.getTotalTurnover() != null) {
							completionTurnover = completionTurnover.add(bill.getTotalTurnover());
						}

						// 已取消的账单流水
					} else if (bill.getOrderStatus() == OrderStatus.ORDER_STATUS_CANCEL) {
						cancellationNumber++;
						if (bill.getTotalTurnover() != null) {
							cancellationTurnover = cancellationTurnover.add(bill.getTotalTurnover());
						}

						// 总的流水
					}
				}

				if (bill.getTotalTurnover() != null) {
					totalTurnover = totalTurnover.add(bill.getTotalTurnover());
				}

			}
			// 订单总数
			totalOrds = countWaterBill.size();

		}

		// 计算订单完成率
		if (totalOrds > 0) {

			rate = new BigDecimal(completionNumber).divide(new BigDecimal(totalOrds), 3, BigDecimal.ROUND_HALF_UP)
					.multiply(new BigDecimal(100)).setScale(1, BigDecimal.ROUND_HALF_UP);

			completionRate = ToolUtil.keepOnePlaces(rate.doubleValue()) + "%";
		} else {
			completionRate = "0%";
		}
		
		// 响应Bean
		ResWaterBillDTO billDTO = new ResWaterBillDTO();
		// 把值存入响应bean
		billDTO.setTotalOrds(totalOrds);
		billDTO.setTotalTurnover(totalTurnover);
		billDTO.setCancellationNumber(cancellationNumber);
		billDTO.setCompletionNumber(completionNumber);
		billDTO.setCancellationTurnover(cancellationTurnover);
		billDTO.setCompletionTurnover(completionTurnover);
		billDTO.setCompletionRate(completionRate);
		return billDTO;
	}

	/**
	 * 
	 * @Title: queryOrderFinishAmount 
	 * @Description: 查询日期内账单完成数详情
	 * @param ReqWaterBillDTO 请求Bean
	 * @return List<ResponseEntity<ResOrderFinishAmountDTO>> 
	 * @author SUNZHICHENG  
	 * @date  2016年8月23日 下午7:35:20
	 * @since v2.4.0
	 * @see 修改hufan
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public ResponseEntity<ResOrderFinishAmount> queryOrderFinishAmount(ReqWaterBillDTO reqWaterBillDTO) {

		ResponseEntity<ResOrderFinishAmount> response = new ResponseEntity<ResOrderFinishAmount>();
		try {
			// 调用dao层方法
			List<OrderFinishAmountDTO> queryOrderFinishAmount = waterBillDao.queryOrderFinishAmount(reqWaterBillDTO);

			// 专车 - 订单完成数详情响应Bean集合
			ResOrderFinishAmount completionNumberList = this.getFinishAmountResponse(queryOrderFinishAmount,PublishCarType.SPECIAL);

			response.setResponseBody(completionNumberList);
			response.setCode(HttpStatus.SC_OK);
			response.setPromptMessage("请求成功");
		} catch (Exception e) {
			logger.error("查询日期内账单详情:" + this.getClass() + ",queryOrderFinishRate()异常!" + e);
			response.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			response.setErrorMessage("查询账单异常");
			response.setPromptMessage("请稍后重试!");

			e.printStackTrace();
		}

		return response;
	}

	/**
	 * 
	 * @Title: getFinishAmountResponse 
	 * @Description: 获取完成数详情集合
	 * @param queryOrderFinishAmount 查询日期内订单完成数详情列表
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResOrderFinishAmount    返回类型
	 * @see hufan
	 */
	public ResOrderFinishAmount getFinishAmountResponse(List<OrderFinishAmountDTO> queryOrderFinishAmount,PublishCarType carType){
		
		// 专车 - 订单完成数详情响应Bean集合
		ResOrderFinishAmount completionNumberList = new ResOrderFinishAmount();

		List<OrderFinishAmountDTO> amountList = new ArrayList<OrderFinishAmountDTO>();

		// 存放日期集合
		List<String> list = new ArrayList<String>();

		// 计算账单数
		if (queryOrderFinishAmount != null && queryOrderFinishAmount.size() > 0) {
			// 获取返回数据中的日期
			for (OrderFinishAmountDTO amountDTO : queryOrderFinishAmount) {
				// 如果集合中不存在此日期，则添加进集合
				if (!list.contains(amountDTO.getDateTime())) {
					list.add(amountDTO.getDateTime());
				}
			}
			// 获取与当前日期相同的数据进行计算,并封装到响应bean
			for (String date : list) {
				// 专车 - 订单完成数详情响应Bean
				OrderFinishAmountDTO orderFinishAmountDTO = new OrderFinishAmountDTO();
				int totalOrds = 0; // 总订单数
				int completionNumber = 0; // 指派订单完成数
				int cancelNumber = 0; // 指派订单取消数

				for (OrderFinishAmountDTO amount : queryOrderFinishAmount) {

					// 如果日期与dao查询的日期相同，总订单数累加，
					if (date.equals(amount.getDateTime())) {
						totalOrds++;
						if(carType == PublishCarType.SPECIAL) {
							// 如果订单状态已完成
							if (amount.getOrderStatus() == OrderEnums.ALREADY_COMPLETE.getIndex()) {
								completionNumber++;
							}
							// 如果订单状态已取消
							if (amount.getOrderStatus() == OrderEnums.ALREADY_CANCEL.getIndex()) {
								cancelNumber++;
							}
						}else if(carType == PublishCarType.TAXI) {
							// 如果订单状态已完成
							if (amount.getOrderStatus() == OrderStatus.ORDER_STATUS_COMPLETE) {
								completionNumber++;
							}
							// 如果订单状态已取消
							if (amount.getOrderStatus() == OrderStatus.ORDER_STATUS_CANCEL) {
								cancelNumber++;
							}
						}
					}

				}
				// 封装响应Bean
				orderFinishAmountDTO.setDateTime(date);
				orderFinishAmountDTO.setTotalOrds(totalOrds);
				orderFinishAmountDTO.setCompletionNumber(completionNumber);
				orderFinishAmountDTO.setCancelNumber(cancelNumber);

				// 添加响应数据到集合
				amountList.add(orderFinishAmountDTO);
			}

		}
		completionNumberList.setCompletionNumberList(amountList);
		
		return completionNumberList;
	}
	
	/**
	 * 
	 * @Title: queryTaxiOrderFinishAmount 
	 * @Description:  查询日期内订单完成数详情(出租车)
	 * @param reqWaterBillDTO 订单完成数详情请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResponseEntity<ResOrderFinishAmount>    返回类型
	 * @see 修改 hufan
	 */
	@Override
	@Transactional(readOnly = true)
	public ResponseEntity<ResOrderFinishAmount> queryTaxiOrderFinishAmount(ReqWaterBillDTO reqWaterBillDTO){
		
		ResponseEntity<ResOrderFinishAmount> response = new ResponseEntity<ResOrderFinishAmount>();
		
		// 获取查询日期内订单完成数详情列表(出租车)
		List<OrderFinishAmountDTO> queryOrderFinishAmount = waterBillDao.queryTaxiOrderFinishAmount(reqWaterBillDTO);

		// 专车 - 订单完成数详情响应Bean集合
		ResOrderFinishAmount completionNumberList = this.getFinishAmountResponse(queryOrderFinishAmount,PublishCarType.TAXI);

		response.setResponseBody(completionNumberList);
		response.setCode(HttpStatus.SC_OK);
		response.setPromptMessage("请求成功");
		
		return response;
	}
	
	/**
	 * 
	 * @Title: queryOrderParticulars 
	 * @Description: 查询日期内账单流水详情
	 * @param 
	 * @return ResponseEntity<ResOrderParticulars> 
	 * @author SUNZHICHENG  
	 * @date  2016年8月24日 上午10:02:27
	 * @since v2.4.0
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseEntity<ResOrderParticulars> queryOrderParticulars(ReqWaterBillDTO reqWaterBillDTO) {

		ResponseEntity<ResOrderParticulars> responseEntity = new ResponseEntity<ResOrderParticulars>();

		try {
			// 调用dao层方法
			List<OrderParticularsDTO> orderParticulars = waterBillDao.queryOrderParticulars(reqWaterBillDTO);

			// 封装流水响应响应Bean
			ResOrderParticulars resOrderParticulars = this.getParticularsResponse(orderParticulars,PublishCarType.SPECIAL);
			
			// 响应
			responseEntity.setCode(HttpStatus.SC_OK);
			responseEntity.setPromptMessage("请求成功");
			responseEntity.setResponseBody(resOrderParticulars);
		} catch (Exception e) {
			logger.error("查询日期内账单流水详情:" + this.getClass() + ",queryOrderParticulars()异常!" + e);
			responseEntity.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			responseEntity.setErrorMessage("查询账单流水异常");
			e.printStackTrace();
		}

		return responseEntity;
	}

	/**
	 * 
	 * @Title: getResponse 
	 * @Description: 获取响应流水详情Bean
	 * @param orderParticulars 流水详情列表
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResOrderParticulars    返回类型
	 * @see 修改 hufan
	 */
	public ResOrderParticulars getParticularsResponse(List<OrderParticularsDTO> orderParticulars,PublishCarType carType){
		
		// 响应Bean集合
		ResOrderParticulars resOrderParticulars = new ResOrderParticulars();

		List<OrderParticularsDTO> orderParticularsList = new ArrayList<OrderParticularsDTO>();

		DecimalFormat df = new DecimalFormat("#####.00"); // 格式化
		// 计算账单流水
		if (orderParticulars != null && orderParticulars.size() > 0) {
			List<String> dateList = new ArrayList<String>();
			// 循环数据，查找相同日期存放到集合
			for (OrderParticularsDTO particulars : orderParticulars) {
				// 集合中不存在此日期，则添加到集合
				if (!dateList.contains(particulars.getDateTime())) {
					dateList.add(particulars.getDateTime());
				}
			}

			// 获取与日期相同的数据进行计算,并封装到响应bean
			for (String date : dateList) {
				// 响应Bean
				OrderParticularsDTO particularsDTO = new OrderParticularsDTO();

				BigDecimal totalTurnover = new BigDecimal(0.00); // 每日指派总流水
				BigDecimal completionTurnover = new BigDecimal(0.00); // 每日指派完成流水
				BigDecimal cancellationTurnover = new BigDecimal(0.00); // 每日指派取消流水
				for (OrderParticularsDTO particular : orderParticulars) {
					// 日期和集合中存在的日期相同
					if (date.equals(particular.getDateTime()) && particular.getTotalTurnover() != null) {
						// 总流水
						totalTurnover = totalTurnover.add(particular.getTotalTurnover());
						if(carType == PublishCarType.SPECIAL) {
							// 已完成流水
							if (particular.getOrderStatus() == OrderEnums.ALREADY_COMPLETE.getIndex()) {
								completionTurnover = completionTurnover
										.add(particular.getTotalTurnover());
							}
							// 已取消流水
							if (particular.getOrderStatus() == OrderEnums.ALREADY_CANCEL.getIndex()) {
								cancellationTurnover = cancellationTurnover
										.add(particular.getTotalTurnover());
							}
						}else if(carType == PublishCarType.TAXI) {
							// 已完成流水
							if (particular.getOrderStatus() == OrderStatus.ORDER_STATUS_COMPLETE) {
								completionTurnover = completionTurnover
										.add(particular.getTotalTurnover());
							}
							// 已取消流水
							if (particular.getOrderStatus() == OrderStatus.ORDER_STATUS_CANCEL) {
								cancellationTurnover = cancellationTurnover
										.add(particular.getTotalTurnover());
							}
						}
					}
				}
				// 封装Bean
				particularsDTO.setDateTime(date);

				particularsDTO.setTotalTurnover(totalTurnover);
				particularsDTO.setCompletionTurnover(completionTurnover);
				particularsDTO.setCancellationTurnover(cancellationTurnover);

				// 将封装的bean添加到集合
				orderParticularsList.add(particularsDTO);
			}

		}
		resOrderParticulars.setOrderFlowList(orderParticularsList);
		
		return resOrderParticulars;
	}
	
	
	/**
	 * 
	 * @Title: queryTaxiOrderParticulars 
	 * @Description: 查询日期内账单订单流水详情（出租车）
	 * @param reqWaterBillDTO 流水查询请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResponseEntity<ResOrderParticulars>    返回类型
	 * @see 修改hufan
	 */
	@Override
	@Transactional(readOnly = true)
	public ResponseEntity<ResOrderParticulars> queryTaxiOrderParticulars(ReqWaterBillDTO reqWaterBillDTO){
		
		ResponseEntity<ResOrderParticulars> response = new ResponseEntity<ResOrderParticulars>();
		
		// 判断客户端请求参数是否为空
		if(!validateParam(response, reqWaterBillDTO)){
			return response;
		}
		
		// 调用dao层方法
		List<OrderParticularsDTO> orderParticulars = waterBillDao.queryTaxiOrderParticulars(reqWaterBillDTO);
		
		// 封装流水响应响应Bean
		ResOrderParticulars resOrderParticulars = this.getParticularsResponse(orderParticulars,PublishCarType.TAXI);
		
		// 响应
		response.setCode(HttpStatus.SC_OK);
		response.setPromptMessage("请求成功");
		response.setResponseBody(resOrderParticulars);
		
		return response;
	}
	
	public boolean validateParam(ResponseEntity<ResOrderParticulars> response,
			ReqWaterBillDTO reqWaterBillDTO) {

		// 判断客户端请求数据是否为空
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(reqWaterBillDTO,
				ReqWaterBillDTO.class);

		if (!valiResult.isAvailable()) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(ConfirmAboardCodeMsg.DATA_NULL_PARAM_MESSAGE);// 客户端请求数据为空
			response.setPromptMessage(ConfirmAboardCodeMsg.APP_ERROR);// 客户端请求数据为空
			response.setNullProperties(valiResult.getNullFieldName());
			logger.error("为空字段为：" + valiResult.getNullFieldName());
			return false;
		}

		return true;
	}
	
	/**
	 * 
	 * @Title: queryOrderFinishRate 
	 * @Description: 查询日期内账单完成率详情
	 * @param ReqWaterBillDTO 请求Bean
	 * @return ResponseEntity<ResOrderFinishRate> 
	 * @author SUNZHICHENG  
	 * @date  2016年8月24日 下午2:33:16
	 * @since v2.4.0
	 * @see 修改 hufan
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public ResponseEntity<ResOrderFinishRate> queryOrderFinishRate(ReqWaterBillDTO reqWaterBillDTO) {

		ResponseEntity<ResOrderFinishRate> response = new ResponseEntity<ResOrderFinishRate>();

		try {
			// 调用Dao层方法
			List<OrderFinishRateDTO> orderFinishRateList = waterBillDao.queryOrderFinishRate(reqWaterBillDTO);

			// 响应集合Bean
			ResOrderFinishRate resOrderFinishRate = this.getOrderFinishRateResponse(orderFinishRateList,PublishCarType.SPECIAL);

			// 添加到响应实体
			response.setResponseBody(resOrderFinishRate);
			response.setCode(HttpStatus.SC_OK);
			response.setPromptMessage("请求成功");
			
		} catch (Exception e) {
			logger.error("查询日期内账单完成率详情:" + this.getClass() + ",queryOrderFinishRate()异常!" + e);
			response.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			response.setErrorMessage("查询账单异常");
			e.printStackTrace();
		}

		return response;
	}

	/**
	 * 
	 * @Title: getOrderFinishRateResponse 
	 * @Description: 获取查询日期内订单完成率详情
	 * @param orderFinishRateList 查询日期内订单完成订单列表
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResOrderFinishRate    返回类型
	 * @see 修改 hufan
	 */
	@SuppressWarnings("unused")
	public ResOrderFinishRate getOrderFinishRateResponse(List<OrderFinishRateDTO> orderFinishRateList,PublishCarType carType){
		
		// 响应bean集合
		List<OrderFinishRateDTO> OrderFinishRateDTOList = new ArrayList<OrderFinishRateDTO>();
		// 响应集合Bean
		ResOrderFinishRate resOrderFinishRate = new ResOrderFinishRate();

		DecimalFormat df = new DecimalFormat("######.000");

		// 订单不为空
		if (orderFinishRateList != null && orderFinishRateList.size() > 0) {
			// 存放日期
			List<String> dateList = new ArrayList<String>();
			// 获取返回数据中的日期值
			for (OrderFinishRateDTO orderFinishRateDTO : orderFinishRateList) {
				// 集合中不存在此日期值,将其添加到集合
				if (!dateList.contains(orderFinishRateDTO.getDateTime())) {
					dateList.add(orderFinishRateDTO.getDateTime());
				}
			}
			// 遍历日期集合，从dao返回的数据中获取信息并计算
			for (String date : dateList) {
				// 响应bean
				OrderFinishRateDTO finishRateDTO = new OrderFinishRateDTO();

				int totalOrds = 0; // 当日订单总数
				int completionNumber = 0; // 当日订单完成数
				BigDecimal rate = new BigDecimal(0.0); // 临时变量
				String completionRate = null; // 订单完成率

				for (OrderFinishRateDTO FinishRateDTO : orderFinishRateList) {
					if (date.equals(FinishRateDTO.getDateTime())) {
						totalOrds++;
						// 已完成
						if(carType == PublishCarType.SPECIAL) {
							if (FinishRateDTO.getOrderStatus() == OrderEnums.ALREADY_COMPLETE.getIndex()) {
								completionNumber++;
							}
						}else if(carType == PublishCarType.TAXI) {
							if (FinishRateDTO.getOrderStatus() == OrderStatus.ORDER_STATUS_COMPLETE) {
								completionNumber++;
							}
						}
					}
				}

				// 计算当日订单完成率
				if (totalOrds > 0) {
					rate = new BigDecimal(completionNumber).divide(new BigDecimal(totalOrds), 3,BigDecimal.ROUND_HALF_UP)
							.multiply(new BigDecimal(100).setScale(1, BigDecimal.ROUND_HALF_UP));
					completionRate = ToolUtil.keepOnePlaces(rate.doubleValue()) + "%";
				}

				// 将处理结果封装到响应bean
				finishRateDTO.setDateTime(date);
				finishRateDTO.setCompletionRate(completionRate);
				// 将响应bean添加到集合
				OrderFinishRateDTOList.add(finishRateDTO);
			}

		}

		// 将集合添加到响应集合bean
		resOrderFinishRate.setCompletionRateList(OrderFinishRateDTOList);
		
		return resOrderFinishRate;
	}
	
	
	/**
	 * 
	 * @Title: queryTaxiOrderFinishRate 
	 * @Description: 查询日期内订单完成率详情（出租车）
	 * @param reqWaterBillDTO 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResponseEntity<ResOrderFinishRate>    返回类型
	 * @see 修改 hufan
	 */
	@Override
	@Transactional(readOnly = true)
	public ResponseEntity<ResOrderFinishRate> queryTaxiOrderFinishRate(ReqWaterBillDTO reqWaterBillDTO){
		
		ResponseEntity<ResOrderFinishRate> response = new ResponseEntity<ResOrderFinishRate>();
		
		// 调用Dao层方法
		List<OrderFinishRateDTO> orderFinishRateList = waterBillDao.queryTaxiOrderFinishRate(reqWaterBillDTO);
		
		// 响应集合Bean
		ResOrderFinishRate resOrderFinishRate = this.getOrderFinishRateResponse(orderFinishRateList,PublishCarType.TAXI);

		// 添加到响应实体
		response.setResponseBody(resOrderFinishRate);
		response.setCode(HttpStatus.SC_OK);
		response.setPromptMessage("请求成功");
		
		return response;
	}
	
}
